package com.link.base.base.common.service;

import com.link.base.base.common.dao.mybatis.mapper.LogMapper;
import com.link.base.base.common.model.Log;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.service.ServiceException;
import com.link.core.cllog.LCLogger;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Blob;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/9 9:22
 * @version 1.0
 */

@Service
@Transactional(rollbackFor = ServiceException.class)
public class LogServiceImpl implements LogService {

    @Resource
    private LogMapper logMapper;

    @Resource
    private KeyGenerateDao keyGenerateDao;

    /**
     * 日志添加
     * @param record
     * @throws ServiceException
     */
    @Override
    public void addLog(Log record) throws ServiceException {

        byte[] logContents = record.getLogContent().getBytes();
        record.setLogContents(logContents);
        record.setId(keyGenerateDao.keyGenerate());
        try {
            logMapper.logInsert(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException(e.getMessage());
        }
    }

    private List<Log> getRecord(List<Map> list) throws ServiceException {
        List<Log> result = new ArrayList<Log>();

        for (Map map : list) {
            Log log = new Log();
            log.setId((Long) map.get("id"));
            log.setTypeCd("type_cd");
            log.setLogLvl("log_lvl");
            log.setCreatedBy((Long) map.get("created_by"));
            log.setLastUpdatedBy((Long) map.get("last_upd_by"));

            Blob blob = (Blob) map.get("log_content");
        }
        return result;
    }

}
